package vpninterceptor

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/go-playground/validator"
	"strconv"
	"time"
	"vpn-management-server/logger"
	"vpn-management-server/network"
	"vpn-management-server/security/code"
	"vpn-management-server/src/model/adminlogmodel"
	"vpn-management-server/src/model/adminmodel"
	"vpn-management-server/src/model/usermodel"
	"vpn-management-server/src/model/vpnmodel"
	"vpn-management-server/src/service/adminlogservice"
	"vpn-management-server/src/service/adminservice"
)

func VpnServerInfoValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	var VpnServerInfo vpnmodel.VpnServerInfo
	err := json.Unmarshal(request.Parameters, &VpnServerInfo)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "UnMarshal Parameters to VpnServerInfo error,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "parse parameters fail"
		return false
	}
	// TODO 验证json字段的合法性
	valid := validator.New()
	err = valid.Struct(VpnServerInfo)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "validate Parameters fail,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "validate parameters fail"
		return false
	}
	// TODO 验证管理员权限等级
	if rank := adminservice.GetAdminRankService(ctx, request, response); rank == adminmodel.UserGroupAdmin {
		request.SetModel(&VpnServerInfo)
		return true
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "have no authority")
		response.Code = code.UNAUTHORIZED
		response.ErrorMsg = "have no authority"
		return false
	}
}

func VpnServerInfoNameValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	var VpnServerID vpnmodel.VpnServerNameInfo
	err := json.Unmarshal(request.Parameters, &VpnServerID)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "UnMarshal Parameters to VpnServerInfo error,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "parse parameters fail"
		return false
	}
	// TODO 验证json字段的合法性
	valid := validator.New()
	err = valid.Struct(VpnServerID)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "validate Parameters fail,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "validate parameters fail"
		return false
	}
	// TODO 验证管理员权限等级
	if rank := adminservice.GetAdminRankService(ctx, request, response); rank == adminmodel.UserGroupAdmin {
		request.SetModel(&VpnServerID)
		return true
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "have no authority")
		response.Code = code.UNAUTHORIZED
		response.ErrorMsg = "have no authority"
		return false
	}
}

func GetVpnServerInfoByUserNameValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	var userNameInfo usermodel.UserNameInfo
	err := json.Unmarshal(request.Parameters, &userNameInfo)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "UnMarshal Parameters to UserNameInfo error,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "parse parameters fail"
		return false
	}

	valid := validator.New()
	err = valid.Struct(&userNameInfo)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "validate Parameters fail,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "validate parameters fail"
		return false
	}

	request.SetModel(&userNameInfo)
	return true
}

func GetVpnServerInfoByIDValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	var VpnServerNameInfo vpnmodel.VpnServerNameInfo
	err := json.Unmarshal(request.Parameters, &VpnServerNameInfo)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "UnMarshal Parameters to VpnServerNameInfo error,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "parse parameters fail"
		return false
	}
	valid := validator.New()
	err = valid.Struct(&VpnServerNameInfo)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "validate Parameters fail,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "validate parameters fail"
		return false
	}
	if rank := adminservice.GetAdminRankService(ctx, request, response); rank == adminmodel.UserGroupAdmin {
		request.SetModel(&VpnServerNameInfo)
		return true
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "have no authority")
		response.Code = code.UNAUTHORIZED
		response.ErrorMsg = "have no authority"
		return false
	}
}

func GetAllVpnServerNameInfosValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	var UGroupID vpnmodel.UGroupID
	err := json.Unmarshal(request.Parameters, &UGroupID)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "UnMarshal Parameters to UGroupID error,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "parse parameters fail"
		return false
	}
	valid := validator.New()
	err = valid.Struct(&UGroupID)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "validate Parameters fail,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "validate parameters fail"
		return false
	}
	if rank := adminservice.GetAdminRankService(ctx, request, response); rank == adminmodel.UserGroupAdmin {
		request.SetModel(&UGroupID)
		return true
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "have no authority")
		response.Code = code.UNAUTHORIZED
		response.ErrorMsg = "have no authority"
		return false
	}
}

func AddVpnServerInfoLogToChan(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	if VpnServerInfo, ok := request.GetModel().(*vpnmodel.VpnServerInfo); ok {
		adminlogservice.AddAdminLogToChan(adminlogservice.ChanAdminLogs, adminlogmodel.AdminLog{
			Name:      adminservice.GetCurrAdminService(ctx, request, response).Name,
			Source:    request.GetRemote(),
			Location:  "地址",
			Behavior:  fmt.Sprintf("添加vpn后台%s的信息", VpnServerInfo.Name),
			TimeStamp: strconv.FormatInt(time.Now().Unix(), 10),
		})
	}
}

func DeleteVpnServerInfoLogToChan(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	if VpnServerInfo, ok := request.GetModel().(*vpnmodel.VpnServerNameInfo); ok {
		adminlogservice.AddAdminLogToChan(adminlogservice.ChanAdminLogs, adminlogmodel.AdminLog{
			Name:      adminservice.GetCurrAdminService(ctx, request, response).Name,
			Source:    request.GetRemote(),
			Location:  "地址",
			Behavior:  fmt.Sprintf("刪除vpn后台%s的信息", VpnServerInfo.Name),
			TimeStamp: strconv.FormatInt(time.Now().Unix(), 10),
		})
	}
}
